/*
 *  Copyright 2011 yuxinlei.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package com.hudson.plugin.result.collector.server.datastore.internal.dao.impl;

import com.hudson.plugin.result.collector.server.datastore.dao.BuildRelationsEntityJpaController;
import com.hudson.plugin.result.collector.server.datastore.dao.exceptions.NonexistentEntityException;
import com.hudson.plugin.result.collector.server.datastore.dao.exceptions.PreexistingEntityException;
import com.hudson.plugin.result.collector.server.datastore.internal.entity.BuildRelationsEntity;

import javax.persistence.*;
import java.util.List;

/**
 * @author yuxinlei
 */
public class BuildRelationsEntityJpaControllerImpl implements BuildRelationsEntityJpaController {

    public BuildRelationsEntityJpaControllerImpl() {
        emf = Persistence.createEntityManagerFactory("reportdb");
    }

    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(BuildRelationsEntity buildRelationsEntity) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(buildRelationsEntity);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findBuildRelationsEntity(buildRelationsEntity.getId()) != null) {
                throw new PreexistingEntityException("BuildRelationsEntity " + buildRelationsEntity + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(BuildRelationsEntity buildRelationsEntity) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            buildRelationsEntity = em.merge(buildRelationsEntity);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                int id = buildRelationsEntity.getId();
                if (findBuildRelationsEntity(id) == null) {
                    throw new NonexistentEntityException("The buildRelationsEntity with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(int id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            BuildRelationsEntity buildRelationsEntity;
            try {
                buildRelationsEntity = em.getReference(BuildRelationsEntity.class, id);
                buildRelationsEntity.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The buildRelationsEntity with id " + id + " no longer exists.", enfe);
            }
            em.remove(buildRelationsEntity);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }


    public BuildRelationsEntity findBuildRelationsEntity(int id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(BuildRelationsEntity.class, id);
        } finally {
            em.close();
        }
    }

    public List<Integer> findParentIdByChildId(Integer childId) {
        EntityManager em = getEntityManager();
        try {
            Query query = em.createNativeQuery(
                    "select r.parent_id from build_relations r where r.child_id = :child_id");
            query.setParameter("child_id", childId);
            List<Integer> childIds = query.getResultList();
            return childIds;
        } finally {
            em.close();
        }

    }


}
